import axios, {AxiosRequestConfig, AxiosRequestHeaders} from 'axios'
// import {getToken, removeToken} from "@/utils/auth";
// import {showMsg} from "@/utils/msg";

const service = axios.create({
    baseURL: '/api',
    timeout: 50000
})

// 请求拦截: 每一次发请求都会做的事情
service.interceptors.request.use((config: AxiosRequestConfig) => {
    const token = window.localStorage.getItem("accessToken");
    if (token) {
        config.headers!['token'] = token
    }
    return config
}, (err) => {
    return Promise.reject(err)
})


//响应拦截
service.interceptors.response.use((response) => {
    const res = response.data
    if (res.code === 404) {
        // showMsg("页面不存在！")
    } else if (res.code === 500) {
        // removeToken()
        // showMsg(res.msg, 'error')
    } else if (res.code === 401) {
        // showMsg(res.msg, 'error')
    } else {
        return res
    }
}, (err) => {
    return Promise.reject(err)
})

export default service
